package com.ruoyi.web.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.domain.bo.QualityContinuousImprovementBo;
import com.ruoyi.quality.domain.vo.QualityContinuousImprovementVo;
import com.ruoyi.quality.service.IQualityContinuousImprovementService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 持续改善
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/continuousImprovement")
public class QualityContinuousImprovementController extends BaseController {

    private final IQualityContinuousImprovementService iQualityContinuousImprovementService;
    private final ISysUserService userService;
    /**
     * 查询持续改善列表
     */
    @SaCheckPermission("quality:continuousImprovement:list")
    @GetMapping("/list")
    public TableDataInfo<QualityContinuousImprovementVo> list(QualityContinuousImprovementBo bo, PageQuery pageQuery) {
        return iQualityContinuousImprovementService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出持续改善列表
     */
    @SaCheckPermission("quality:continuousImprovement:export")
    @Log(title = "持续改善", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QualityContinuousImprovementBo bo, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        List<QualityContinuousImprovementVo> list = iQualityContinuousImprovementService.queryList(bo);
        list.forEach(item->{

            Long leaderId = item.getLeaderId();
            if (leaderId!=null){
                SysUser leader = userService.selectUserById(leaderId);
                item.setLeaderName(leader.getUserName());
            }

            Long userId = item.getUserId();
            SysUser user = userService.selectUserById(userId);
            item.setUserName(user.getNickName());

            Long groupId = item.getGroupId();
            if (groupId!=null) {
                SysUser group = userService.selectUserById(groupId);
                item.setGroupName(group.getUserName());
            }

            Long managerId = item.getManagerId();
            if (managerId!=null){
                SysUser manager = userService.selectUserById(managerId);
                item.setManagerName(manager.getUserName());
            }

        });
        ExcelUtil.exportTemplate(CollUtil.newArrayList(map, list), "单列表.xlsx", "excel/持续改善汇总表.xlsx", response);
    }

    /**
     * 获取持续改善详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("quality:continuousImprovement:query")
    @GetMapping("/{id}")
    public R<QualityContinuousImprovementVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iQualityContinuousImprovementService.queryById(id));
    }

    /**
     * 新增持续改善
     */
    @SaCheckPermission("quality:continuousImprovement:add")
    @Log(title = "持续改善", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QualityContinuousImprovementBo bo) {
        return toAjax(iQualityContinuousImprovementService.insertByBo(bo));
    }

    /**
     * 修改持续改善
     */
    @SaCheckPermission("quality:continuousImprovement:edit")
    @Log(title = "持续改善", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QualityContinuousImprovementBo bo) {
        return toAjax(iQualityContinuousImprovementService.updateByBo(bo));
    }

    /**
     * 删除持续改善
     *
     * @param ids 主键串
     */
    @SaCheckPermission("quality:continuousImprovement:remove")
    @Log(title = "持续改善", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iQualityContinuousImprovementService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
